<template>
  <div>
    <el-dialog :title="title" :visible.sync="isShowDialogComponent" width="50%" :before-close="handleClose">
      <el-form ref="formRef" :model="form" label-width="100px" :rules="formRules" v-loading="loading" element-loading-text="拼命加载中......" element-loading-spinner="el-icon-loading">
        <el-form-item label="启用学期 :">
          <span>{{ currentSemesterName }}</span>
        </el-form-item>

        <el-form-item label="选择学科 :">
          <el-select v-model="form.SubjectID" :disabled="judgeId!==0">
            <el-option v-for="(item,index) in subjectList" :key="index" :label="item.Text" :value="item.Value">
            </el-option>
          </el-select>
        </el-form-item>

        <div class="name-price">
          <el-form-item label="课程名称 :" prop="ClassName">
            <el-input v-model="form.ClassName"></el-input>
          </el-form-item>
          <el-form-item label="课程定价 :" class="second" prop="Price">
            <el-input v-model="form.Price"></el-input>
          </el-form-item>
        </div>

        <div class="count-hours">
          <el-form-item label="容纳人数 :" prop="Accommodate">
            <el-input v-model="form.Accommodate"></el-input>
          </el-form-item>
          <el-form-item label="课程时长 :" class="second" prop="ClassHour">
            <el-input v-model="form.ClassHour"></el-input>
          </el-form-item>
        </div>

        <el-form-item label="课程描述 :" prop="Describe">
          <el-input v-model="form.Describe" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="课程图片 :">
          <upload-img @showImage="showImage" class="image-style" :judge-id="judgeId" :image="form.Image" :is-show-letter="true" :width="16" :height="9">
          </upload-img>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose" :disabled="loading">取 消</el-button>
        <el-button type="primary" @click="submitEvent" :disabled="loading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
//引入封装的接口的类名
import CompanySubject from "../../../model/companySubject";
import Semester from "../../../model/semester";
import CompanyCurriculum from "../../../model/companyCurriculum";
//引入封装的验证规则，包括价格、手机等的一些特殊验证
import ProofRules from "../../../ck/util/proofRules";
//引入封装好的图片上传+裁剪的组件
import UploadImg from "../../../components/base/uploadPictures/UploadImg";
export default {
  name: "invoice-mail-dialog",
  components: {
    UploadImg,
  },
  props: {
    title: String,
    isShowDialogComponent: Boolean,
    judgeId: Number,
    subjectList: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      loading: false,
      //图片裁剪对话框的显示与隐藏
      dialogVisible: false,
      formRules: {
        ClassName: ProofRules.requiredFieldAndLength("课程名称", 2, 20),
        Price: ProofRules.PriceMethod("课程价格"),
        Describe: ProofRules.requiredFieldAndLength("课程描述", 2, 200),
        Accommodate: ProofRules.accommodateMethod("课程容纳人数"),
        ClassHour: ProofRules.classHoursMethod("课程时长"),
      },
      //保存学期名称
      currentSemesterName: "",
      form: {
        SubjectID: null,
        ClassName: "",
        Describe: "",
        Image: "",
        Price: "",
        Accommodate: "",
        ClassHour: "",
      },
      //保存图片上传的拼接路径
      imgUrl: "api/Common/CommonMethod/PostUploadImage",
    };
  },
  created() {
    //获取当前正在启用的学期信息
    // this.getCurrentSemester();
    // this.judgeEditButton();
  },

  methods: {
    handleClose(payload) {
      this.$emit("handleClose", payload);
    },
    //判断点击的是否是编辑按钮
    async judgeEditButton() {
      // if (this.judgeId !== 0) {
      //   const res = await CompanyCurriculum.SearchModelDetails({
      //     ID: this.judgeId,
      //   });
      //   this.form = res;
      // }
    },
    //图片上传和裁剪
    showImage(showImageUrl) {
      this.form.Image = showImageUrl;
    },
    //获取当前启用学期的信息
    async getCurrentSemester() {
      const res = await Semester.GetCurrentSemester();
      this.form.SemesterID = res[0].Value;
      this.currentSemesterName = res[0].Text;
    },
    submitEvent() {
      //提交前先进行表单验证
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          if (this.form.Image === "") {
            this.$message.error("课程图片为必传项");
          } else if (this.form.SubjectID === 0) {
            this.$message.error("学科为必选项");
          } else {
            try {
              //判断是否时添加操作
              if (this.judgeId === 0) {
                this.loading = true;
                await CompanyCurriculum.Add(this.form);
                this.loading = false;
                this.$message.success("添加课程成功");
                this.handleClose("submitButton");
              } else {
                //如果是编辑操作
                this.loading = true;
                await CompanyCurriculum.Update(this.form);
                this.loading = false;
                this.$message.success("编辑课程信息成功");
                this.handleClose("submitButton");
              }
            } catch (err) {
              this.loading = false;
            }
          }
        } else {
          this.$message.error("请填写表单必填项");
        }
      });
    },
  },
};
</script>

<style scoped>
.edit-show-image {
  height: 150px;
  width: 150px;
}
.name-price {
  display: flex;
}
.count-hours {
  display: flex;
}
.second {
  margin-left: 100px;
}
/deep/ .el-textarea__inner {
  width: 80%;
}
</style>
